import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final List<BottomNavigationBarItem> bottomNavItems = const [
    BottomNavigationBarItem(
      backgroundColor: Colors.blue,
      icon: Icon(Icons.home),
      label: '首页',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.green,
      icon: Icon(Icons.message),
      label: '消息',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.amber,
      icon: Icon(Icons.shopping_cart),
      label: '购物车',
    ),
    BottomNavigationBarItem(
      backgroundColor: Colors.red,
      icon: Icon(Icons.person),
      label: '我',
    ),
  ];

  final List<Widget> pages = const [
    Center(child: Text('Home',    style: TextStyle(fontSize: 50))),
    Center(child: Text('Message', style: TextStyle(fontSize: 50))),
    Center(child: Text('Cart',    style: TextStyle(fontSize: 50))),
    Center(child: Text('Profile', style: TextStyle(fontSize: 50))),
  ];

  int currentIndex = 0;   // ✅ 直接初始化，避免 late / 可空类型

  @override
  void initState() {
    super.initState();
    // 如需额外逻辑再写
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('底部导航'),
        leading: const Icon(Icons.menu),
        centerTitle: true,
        actions: const [Icon(Icons.settings)],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomNavItems,
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        onTap: _changePage,
      ),
      body: pages[currentIndex],
    );
  }

  void _changePage(int index) {
    if (index != currentIndex) {
      setState(() => currentIndex = index);
    }
  }
}